package com.foreverwin.mes.lims.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.common.constants.HandleBOConstants;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.lims.enums.InspectObject;
import com.foreverwin.mes.lims.mapper.InspectPlanDetailMapper;
import com.foreverwin.mes.lims.model.InspectPlanDetail;
import com.foreverwin.mes.lims.model.InspectProject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.lims.service.InspectPlanService;
import com.foreverwin.mes.lims.model.InspectPlan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Levy
 * @since 2020-02-20
 */
@Controller
@RequestMapping("/inspectPlans")
public class InspectPlanController {
    private final Logger logger = LoggerFactory.getLogger(InspectPlanController.class);

    @Autowired
    public InspectPlanService inspectPlanService;
    @Autowired
    public InspectPlanDetailMapper inspectPlanDetailMapper;
    /**
     * 保存检验项目
     *
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doSave", method = RequestMethod.POST)
    public AjaxResult stationCertificationSave(@RequestBody JSONObject jsonObject) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();

        try {

            //参数
            String object = null;
            String workCenter = null;
            String operation = null;
            String itemGroup = null;
            String resrce = null;

            Map<String, Object> map = (Map<String, Object>) jsonObject.get("plan");
            if (map == null) {
                throw BusinessException.build("参数传递错误");
            }
            if (!map.containsKey("object") || StringUtils.isBlank(map.get("object") + "")) {
                throw BusinessException.build("检验计划对象不能为空");
            }


            object = (String) map.get("object");
            workCenter = (String) map.get("workCenter");
            operation = (String) map.get("operation");
            itemGroup = (String) map.get("itemGroup");
            resrce = (String) map.get("resource");

            /**
             * 检验计划主表数据组装
             */
            InspectPlan inspectPlan = new InspectPlan();
            inspectPlan.setSite( site );
            inspectPlan.setObject( object );
            inspectPlan.setWorkCenter( workCenter );
            inspectPlan.setOperation( operation );
            inspectPlan.setResrce( resrce );
            inspectPlan.setItemGroup( itemGroup );
            inspectPlan.setCreatedUser( user );

            JSONArray planArray = jsonObject.getJSONArray("inspectionPlanList");

            if ( planArray ==null ||planArray.size() <=0){
                throw BusinessException.build("缺少检验计划明细!");
            }
            inspectPlanService.doSave( inspectPlan ,planArray );
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success("检验计划维护成功！");
    }

    /**
     * 查询检验计划
     *
     * @param inspectPlan
     * @return
     */
    @ResponseBody
    @GetMapping("/getInspectPlan")
    public AjaxResult getStationAndCertification(InspectPlan inspectPlan) {
        Map<String, Object> result = new HashMap<String, Object>();
        String site = CommonMethods.getSite();
        try {
            String planBo ;
            String object = inspectPlan.getObject();
            if (InspectObject.RES.name().equals( object )){
                   planBo = HandleBOConstants.INSPECT_PLAN_BO+":"+inspectPlan.getResrce();
            }else if (InspectObject.ENV.name().equals( object )){

                planBo = HandleBOConstants.INSPECT_PLAN_BO+":"+inspectPlan.getWorkCenter();
            }else {
                planBo = HandleBOConstants.INSPECT_PLAN_BO+":"+inspectPlan.getItemGroup();
            }
           InspectPlan oldModel = inspectPlanService.selectById( planBo );

            if ( oldModel == null ){
                throw BusinessException.build("未找到检验计划!");
            }

            //查询计划明细
            Map paramMap = new HashMap();
            paramMap.put("PLAN_BO",planBo);
            List<InspectPlanDetail> inspectPlanDetails = inspectPlanDetailMapper.selectByMap( paramMap );


            result.put("inspectPlan", oldModel);
            result.put("planList", inspectPlanDetails);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

}